home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / libc / dos / dir / findfirs.c next >
Encoding:
C/C++ Source or Header  |  1995-10-29  |  2.6 KB  |  98 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <go32.h>
  7. #include <dpmi.h>
  8. #include <dir.h>
  9. #include <libc/dosio.h>
  10.  
  11. int
  12. findfirst(const char *pathname, struct ffblk *ffblk, int attrib)
  13. {
  14.   __dpmi_regs r;
  15.   int pathlen;
  16.  
  17.   if (pathname == 0 || ffblk == 0)
  18.   {
  19.     errno = EACCES;
  20.     return -1;
  21.   }
  22.  
  23.   pathlen = strlen(pathname) + 1;
  24.  
  25.   _put_path(pathname);
  26.   if(_USE_LFN) {
  27.  
  28.     /* si = 1 indicates DOS style dates, 0 means Win32 type dates.
  29.        DOS style dates are broken in some Win95 betas, build for either.
  30.        Release works with DOS date, it's faster, so use it. */
  31.     #define USEDOSDATE 1
  32.     #if USEDOSDATE == 1
  33.       #define _Win32_to_DOS (long)
  34.     #else
  35.       extern long _Win32_to_DOS(long long WinTime);
  36.     #endif
  37.  
  38.     r.x.ax = 0x714e;
  39.     r.x.cx = attrib;
  40.     r.x.dx = __tb_offset;
  41.     r.x.ds = __tb_segment;
  42.     r.x.di = __tb_offset + pathlen;
  43.     r.x.es = r.x.ds;
  44.     r.x.si = USEDOSDATE;
  45.     __dpmi_int(0x21, &r);
  46.     if(!(r.x.flags & 1)) {
  47.       struct ffblklfn ffblk32;
  48.       /* Recover results */
  49.       dosmemget(__tb+pathlen, sizeof(struct ffblklfn), &ffblk32);
  50.  
  51.       ffblk->ff_attrib = (char)ffblk32.fd_attrib;
  52.       *(long *)(&ffblk->ff_ftime) = _Win32_to_DOS(ffblk32.fd_mtime);
  53.       ffblk->ff_fsize = ffblk32.fd_size;
  54.       strcpy(ffblk->ff_name, ffblk32.fd_longname);
  55.       strcpy(ffblk->lfn_magic, "LFN32");
  56.  
  57.       /* If no wildcards, close the handle */
  58.       if(!strchr(pathname,'*') && !strchr(pathname,'?')) {
  59.         r.x.bx = r.x.ax;
  60.         r.x.ax = 0x71a1;
  61.         __dpmi_int(0x21, &r);
  62.         r.x.ax = 0;
  63.       }
  64.         
  65.       ffblk->lfn_handle = r.x.ax;
  66.       *(long *)(&ffblk->lfn_ctime) = _Win32_to_DOS(ffblk32.fd_ctime);
  67.       *(long *)(&ffblk->lfn_atime) = _Win32_to_DOS(ffblk32.fd_atime);
  68.  
  69.       return 0;
  70.     }
  71.   } else {
  72.  
  73.     #define _sizeof_dos_ffblk 44
  74.     /* There will be a _sizeof_dos_ffblk character return value from findfirst 
  75.        in the DTA.  Put the file name before this.  First set the DTA to be 
  76.        transfer buffer. */
  77.  
  78.     r.x.dx = __tb_offset + pathlen;
  79.     r.x.ds = __tb_segment;
  80.     r.h.ah = 0x1a;
  81.     __dpmi_int(0x21, &r);
  82.  
  83.     r.h.ah = 0x4e;
  84.     r.x.dx = __tb_offset;
  85.     r.x.ds = __tb_segment;
  86.     r.x.cx = attrib;
  87.     __dpmi_int(0x21, &r);
  88.     if(!(r.x.flags & 1)) {
  89.       /* Recover results */
  90.       dosmemget(__tb+pathlen, _sizeof_dos_ffblk, ffblk);
  91.       return 0;
  92.     }
  93.   }
  94.  
  95.   errno = __doserr_to_errno(r.x.ax);
  96.   return errno;
  97. }
  98.